package com.example.crm01.service.impl.system;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.crm01.entity.Business;
import com.example.crm01.entity.system.ServicePackage;
import com.example.crm01.entity.system.SystemBusiness;
import com.example.crm01.mapper.BusinessMapper;
import com.example.crm01.mapper.system.ServicePackageMapper;
import com.example.crm01.mapper.system.SystemBusinessMapper;
import com.example.crm01.service.BusinessService;
import com.example.crm01.service.ServicePackageService;
import com.example.crm01.service.SystemBusinessService;
import com.example.crm01.service.form.BaseIdForm;
import com.example.crm01.service.form.system.SystemBusinessAddForm;
import com.example.crm01.service.form.system.SystemBusinessListForm;
import com.example.crm01.service.vo.system.ServicePackageDTO;
import com.example.crm01.utils.StringUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class SystemBusinessServiceImpl extends ServiceImpl<SystemBusinessMapper, SystemBusiness> implements SystemBusinessService {
    public final SystemBusinessMapper systemBusinessMapper;
    public final ServicePackageService servicePackageService;
    public final ServicePackageMapper servicePackageMapper;

    public SystemBusinessServiceImpl(SystemBusinessMapper systemBusinessMapper, ServicePackageService servicePackageService, ServicePackageMapper servicePackageMapper) {
        this.systemBusinessMapper = systemBusinessMapper;
        this.servicePackageService = servicePackageService;

        this.servicePackageMapper = servicePackageMapper;
    }


    @Override
    public Page<SystemBusiness> listSystemBusiness(SystemBusinessListForm form) {
        Page<SystemBusiness> page = new Page<>(form.getPageNum(), form.getPageSize());

        LambdaQueryWrapper<SystemBusiness> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(form.getName())) {
            queryWrapper.like(SystemBusiness::getName, form.getName());
        }
        if (ObjectUtils.isNotEmpty(form.getRegionId()) && StringUtils.isNotEmpty(form.getRegionName()) ) {
            queryWrapper.like(SystemBusiness::getRegionId, form.getRegionId());
            queryWrapper.like(SystemBusiness::getRegionName, form.getRegionName());
        }

        if (ObjectUtils.isNotEmpty(form.getBuyType())) {
            queryWrapper.eq(SystemBusiness::getBuyType, form.getBuyType());
        }

        queryWrapper.ne(SystemBusiness::getDel_flag, 2);
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public Boolean addSystemBusiness(SystemBusinessAddForm form) {
        SystemBusiness systemBusiness  = new SystemBusiness();
        systemBusiness.setName(form.getName());
        systemBusiness.setRegionId(form.getRegionId());
        systemBusiness.setRegionName(form.getRegionName());
        systemBusiness.setBuyType(form.getBuyType());

        List<ServicePackage> servicePackages = servicePackageMapper.selectBatchIds(form.getServicePackageIds());
        List<ServicePackageDTO> collect = servicePackages.stream().map(entity -> {
            ServicePackageDTO dto = new ServicePackageDTO();
            dto.setId(entity.getId());
            dto.setName(entity.getName());
            dto.setPrice(entity.getPrice());
            return dto;
        }).collect(Collectors.toList());
        systemBusiness.setServicePackage(JSON.toJSONString(collect));

        //-----
        systemBusiness.setCreateBy("1");
        systemBusiness.setCreateTime(LocalDateTime.now());
        systemBusiness.setUpdateBy("1");
        systemBusiness.setUpdateTime(LocalDateTime.now());
        systemBusiness.setDel_flag(0);
        int insert = systemBusinessMapper.insert(systemBusiness);
        if (insert > 0) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public Boolean updateSystemBusiness(SystemBusinessAddForm form) {
        if (ObjectUtils.isEmpty(form.getId())){
            throw new RuntimeException("修改基础业务信息，主键id不能为空");
        }

        SystemBusiness systemBusiness = systemBusinessMapper.selectById(form.getId());
        systemBusiness.setName(form.getName());
        systemBusiness.setRegionId(form.getRegionId());
        systemBusiness.setRegionName(form.getRegionName());
        systemBusiness.setBuyType(form.getBuyType());

        List<ServicePackage> servicePackages = servicePackageMapper.selectBatchIds(form.getServicePackageIds());
        List<ServicePackageDTO> collect = servicePackages.stream().map(entity -> {
            ServicePackageDTO dto = new ServicePackageDTO();
            dto.setId(entity.getId());
            dto.setName(entity.getName());
            dto.setPrice(entity.getPrice());
            return dto;
        }).collect(Collectors.toList());
        systemBusiness.setServicePackage(JSON.toJSONString(collect));

        //-----
        systemBusiness.setUpdateBy("1");
        systemBusiness.setUpdateTime(LocalDateTime.now());
        systemBusiness.setDel_flag(0);
        int insert = systemBusinessMapper.updateById(systemBusiness);
        if (insert > 0) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public Boolean deleteSystemBusiness(BaseIdForm form) {
        SystemBusiness systemBusiness = systemBusinessMapper.selectById(form.getId());
        if (ObjectUtils.isEmpty(systemBusiness)){
            throw new RuntimeException("基础业务信息不存在删除失败");
        }
        systemBusiness.setUpdateBy("1");
        systemBusiness.setUpdateTime(LocalDateTime.now());
        systemBusiness.setDel_flag(2);
        int update = systemBusinessMapper.updateById(systemBusiness);
        if (update > 0) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }
}
